import 'package:changxindianzishangwu/model/collection_label_model.dart';
import 'package:changxindianzishangwu/pages/my/collect_fragment_page.dart';
import 'package:changxindianzishangwu/pages/my/collect_search_page.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:changxindianzishangwu/widget/icon_text_button.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

///收藏列表页面
class CollectPage extends StatefulWidget {
  const CollectPage({Key key}) : super(key: key);

  @override
  _CollectPageState createState() => _CollectPageState();
}

class _CollectPageState extends State<CollectPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController; //需要定义一个Controller
  //文本识别出链接 可点击跳转
  ///1.图片   2.视频   5.文本   4.语音
  List<CollectionLabelModel> _tabs = [
    CollectionLabelModel("全部",0),
    CollectionLabelModel("图片",1),
    CollectionLabelModel("视频",2),
    CollectionLabelModel("文本",5),
    CollectionLabelModel("语音",4),
  ];

  ///只能是本地地址

  //是否选择
  bool isSelect = false;

  @override
  void initState() {
    super.initState();
    // 创建Controller
    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle =
        TextStyle(fontSize: sp(14), color: AppColor.colorDefaultText);

    var _tab = TabBar(
        labelColor: AppColor.colorTheme,
        unselectedLabelColor: AppColor.colorDefaultText,
        labelStyle: textStyle,
        unselectedLabelStyle: textStyle,
        isScrollable: true,
        //生成Tab菜单
        controller: _tabController,
        tabs: _tabs.map((e) => Tab(text: e.title)).toList());
    // return test(textStyle, _tab);
    return CustomScrollView(
      slivers: [
        _appBar(context, textStyle),
        SliverToBoxAdapter(
          child:  Container(
            color: Colors.white,
            child: Container(
              width: double.infinity,
              margin: EdgeInsets.only(left: 15,right: 15),
              height: height(32),
              decoration: BoxDecoration(
                color: AppColor.bg,
                borderRadius: BorderRadius.circular(6),
              ),
              child: TextButton.icon(
                onPressed: () {
                  Get.to(CollectSearchPage());
                },
                icon: Icon(
                  Icons.search,
                  color: AppColor.colorText85,
                  size: 12,
                ),
                label: Text(
                  "搜索",
                  style: textStyle,
                ),style: ButtonStyle(padding:MaterialStateProperty.all(EdgeInsets.zero)),),
            ),
          ),
        ),
        SliverPersistentHeader(
          pinned: true,
          delegate: StickyTabBarDelegate(
            child: _tab,
          ),
        ),
        SliverFillRemaining(
          child: TabBarView(
            controller: _tabController,
            children: _tabs.map((e) {
              return CollectFragmentPage(
                type: e.index,
                isSelect: isSelect,
              );
            }).toList(),
            physics: isSelect ? NeverScrollableScrollPhysics() : null,
          ),
        ),
      ],
    );
  }

  SliverAppBar _appBar(BuildContext context, TextStyle textStyle) {
    return SliverAppBar(
      title: Text(
        "收藏",
        style: TextStyle(fontSize: 16, color: AppColor.colorDefaultText),
      ),
      centerTitle: true,
      leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: AppColor.colorDefaultText,
          ),
          onPressed: () {
            Navigator.of(context).pop();
          }),
      // expandedHeight: height(100),
      pinned: true,
      // flexibleSpace: Align(
      //   alignment: Alignment.bottomCenter,
      //   child: Container(
      //     width: MediaQuery.of(context).size.width - 30,
      //     height: height(32),
      //     decoration: BoxDecoration(
      //       color: AppColor.bg,
      //       borderRadius: BorderRadius.circular(6),
      //     ),
      //     child: TextButton.icon(
      //         onPressed: () {},
      //         icon: Icon(
      //           Icons.search,
      //           color: AppColor.colorText85,
      //           size: 12,
      //         ),
      //         label: Text(
      //           "搜索",
      //           style: textStyle,
      //         ),style: ButtonStyle(padding:MaterialStateProperty.all(EdgeInsets.zero)),),
      //   ),
      // ),
      actions: [
        Padding(
          padding: EdgeInsets.only(right: 10.0),
          child: Center(
            child: GestureDetector(
              onTap: () {
                setState(() {
                  isSelect = !isSelect;
                });
              },
              child: Text(isSelect ? "取消" : "多选",
                  style: TextStyle(
                      fontSize: 15, color: AppColor.colorDefaultText)),
            ),
          ),
        )
      ],
    );
  }
}

class StickyTabBarDelegate extends SliverPersistentHeaderDelegate {
  final TabBar child;

  StickyTabBarDelegate({@required this.child});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Theme.of(context).backgroundColor,
      child: this.child,
    );
  }

  @override
  double get maxExtent => this.child.preferredSize.height;

  @override
  double get minExtent => this.child.preferredSize.height;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
